home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-16 | 3.6 KB | 138 lines | [TEXT/CWIE] |
-
- //
- // LetterTree.h
- // Copyright © 1994 - 1998 Apple Computer, Inc.
-
- // DESCRIPTION:
-
- // This file implements the classes LetterTree and LetterNodeChain.
- // LetterTree stores words and can perform lookups relatively quickly.
- // It is relatively bulky and therefore probably not suitable for very large collections.
-
-
- // IMPLEMENTATION:
-
- // LetterTree stores an array (a-z or A-Z) of initial letters.
- // Each of those, when not NULL, is a pointer which points to an instance
- // of LetterNodeChain.
-
- // LetterNodeChain encodes a single letter, a next node, and a child node.
- // Instances in a chain (via next) are at the same level of the tree (letters at the same position
- // in the word).
- // Instances in a chain (via child) encode characters one level later in the word.
-
- // A sample tree encoding the words "about","around", "art", and "duh" would be:
- // [TLNC == LetterNodeChain]
-
- // <LetterTree>
- // [A] -> <TLNC: 'B'>
- // child-> <TLNC: 'O'>
- // child-> <TLNC: 'U'>
- // child-> <TLNC: 'T'>
- // child-> <TLNC: '\0'> (path encoding "ABOUT")
- // next-> <TLNC: 'R'>
- // child-> <TLNC: 'T'>
- // child-> <TLNC: '\0'> (path encoding "ART")
- // next-> <TLNC: 'O'>
- // child-> <TLNC: 'U'>
- // child-> <TLNC: 'N'>
- // child-> <TLNC: 'D'>
- // child-> <TLNC: '\0'>
- // (path encoding "AROUND")
- // [D] -> <TLNC: 'U'>
- // child-> <TLNC: 'H'>
- // child-> <TLNC: '\0'> (path encoding "DUH")
- //
- //
- // This class assumes words are canonized (upper/lower settable with a #define).
- //
- // currently uses an unsigned 8-bit char--not compatible with 16-bit characters.
-
- #pragma once
-
- #ifndef LETTERTREE_h
- #define LETTERTREE_h
-
- #pragma import on
-
- #if PRAGMA_STRUCT_ALIGN
- #pragma options align=power
- #endif
-
- #include "IAStorable.h"
-
- class IACharStream;
- class StringTerm;
-
- #pragma IA_BEGIN_EXPORTS
-
- class LetterNodeChain {
- public:
-
- LetterNodeChain( byte letterParam );
- void DeleteChain();
-
- void Add( byte *newWord, uint32 wordLen );
- bool Contains( byte *word );
-
- // returns the number of bytes in an objects serialization
- uint32 StoreSize( uint32 currentWordSize );
- // serializes the object to output, writing StoreSize() bytes to output
- void Store(IAOutputBlock* output, byte *currentWord, unsigned short position);
-
- private:
- unsigned char letter;
- LetterNodeChain *child;
- LetterNodeChain *next;
-
- LetterNodeChain *ProperNodeInChain( byte letterParam );
- LetterNodeChain *ProperNodeInChainAddIfNone( byte letterParam );
- void AddChildren( byte *newWord, uint32 wordLen );
- bool ChildContains( byte *word );
-
-
- };
-
- const unsigned short kHowManyLetters = ('z' - 'a' + 1);
-
- class LetterTree : public IAStorable {
-
- public:
-
- LetterTree();
- LetterTree( IAInputBlock *block );
- ~LetterTree() { EmptyTree(); };
- void EmptyTree();
-
- virtual IAStorable* DeepCopy() const;
-
- // returns the number of bytes in an objects serialization
- virtual uint32 StoreSize() const;
- // serializes the object to output, writing StoreSize() bytes to output
- virtual void Store(IAOutputBlock* output) const;
- // deserializes and returns a new object -- reading StoreSize() bytes from input
- virtual IAStorable* Restore(IAInputBlock* input) const;
-
- void LoadFromFile( char *filePath );
- void LoadFromStream( IACharStream *stream );
-
- void Add( StringTerm *newWord );
- bool Contains( byte *word );
- bool Contains( StringTerm *term );
-
- static const unsigned short kMaxCharsInWord;
-
- private:
- LetterNodeChain *children[kHowManyLetters];
-
- };
-
- #pragma IA_END_EXPORTS
-
- #if PRAGMA_STRUCT_ALIGN
- #pragma options align=reset
- #endif
-
- #pragma import reset
-
- #endif